package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcxy360.sandtable.domain.Problem;
import com.zcxy360.sandtable.service.ProblemService;

/**
 * 问题接口
 * @author 盛亚辉
 *
 */
@RequestMapping("problem")
@RestController
public class ProblemController {

	@Autowired
	private ProblemService problemService;

	/**
	 * 获取问题
	 * @param problemId 问题id
	 * @param deep 0代表基本数据,1代表扩展数据
	 * @return 问题对象
	 */
	@RequiresAuthentication
	@GetMapping("{problemId}")
	public Object getByProblemId(@PathVariable("problemId") Integer problemId,@RequestParam(defaultValue="0") Integer deep) {
		return  problemService.getByProblemId(problemId,deep);
	}
	
	/**
	 * 获取问题集合
	 * @param problem 问题字段
	 * @param key 关键字 
	 * @param orderByClause 排序规则
	 * @param sign 查询标识
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 问题集合
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@GetMapping
	public Object list(Problem problem,String key,String orderByClause,Integer sign,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return problemService.list(problem,key,orderByClause,sign,pageNum,pageSize,navigatePages);
	}
	
	/**
	 * 新增问题
	 * @param problem 问题字段
	 * @return 问题对象
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PostMapping
	public Object post(Problem problem) {
		return problemService.post(problem);
	}
	
	/**
	 * 更新问题
	 * @param problem 问题字段
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PutMapping
	public Object update(Problem problem) {
		return problemService.update(problem);
	}
	
	/**
	 * 删除问题
	 * @param problemIds 问题id数组
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@DeleteMapping("{problemIds}")
	public Object delete(@PathVariable("problemIds") int[] problemIds) {
		return problemService.delete(problemIds);
	}
	
	/**
	 * 检查名称
	 * @param name 问题名称
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@GetMapping("checkName")
	public Object checkName(@RequestParam("name") String name) {
		return problemService.checkName(name);
	}
	
}
